import SimpleSchema from 'simpl-schema';
import { Schemas } from './Schemas';
import { Random } from 'meteor/random';

Schemas.Channel = new SimpleSchema({
  _id: {
    type: String,
    autoValue: function () {
      if (this.isInsert) {
        return Random.id(18);
      }
    }
  },
  owner: {
    type: Object,
    blackbox: true
  },
  channelId: {
    type: String,
    min: 4,
    max: 16,
    regEx: /^[a-zA-Z0-9_]{4,16}$/,
    label: '频道ID'
  },
  name: {
    type: String,
    min: 2,
    max: 40
  },
  type: {
    type: Object,
    blackbox: true
  },
  'type.name': {
    type: String
  },
  'type.displayName': {
    type: String
  },
  policy: {
    type: Object,
    blackbox: true
  },
  'policy.name': {
    type: String,
    allowedValues: ['public', 'private']
  },
  'policy.displayName': {
    type: String,
    allowedValues: ['公开的', '私密的']
  },
  'policy.apply': {
    type: Boolean,
  },
  numberOfPeople: {
    type: Object,
    blackbox: true
  },
  'numberOfPeople.name': {
    type: String
  },
  'numberOfPeople.displayName': {
    type: String
  },
  cover: {
    type: String,
    required: false
  },
  theme: {
    type: String,
    required: false
  },
  notes: {
    type: String,
    required: false,
    max: 400,
    label: '频道描述'
  },
  createdAt: {
    type: Date,
    autoValue: function () {
      if (this.isInsert) {
        return new Date();
      } else if (this.isUpsert) {
        return { $setOnInsert: new Date() };
      } else {
        this.unset();
      }
    }
  },
  updatedAt: {
    type: Date,
    autoValue: function () {
      if (this.isUpdate) {
        return new Date();
      }
    },
    denyInsert: true,
    optional: true
  },
});